package com.sise.wangzhan.command;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;

import java.util.concurrent.TimeUnit;

/**
 * @Classname FallbackTest
 * @Description TODO
 * @Date 2020/4/22 21:49
 * @Created by wangzhan
 */
public class FallbackTest extends HystrixCommand<String > {
    private Integer id;

    public FallbackTest(Integer id){
        super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
        this.id = id;
    }

    @Override
    protected String run() throws Exception {
        if (id % 2 == 0 && id <= 10){   //让小于等于10的偶数返回
            return "running run():"+id;
        }else { //让奇数或大于1的书进入fallback
            TimeUnit.MILLISECONDS.sleep(2000);
            return "";
        }
    }

    @Override
    protected String getFallback(){
        System.out.println("running getFallback():"+id);
        return "";
    }

}
